﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/05/27
 * 
 * 功能说明：复杂功能模块管理页面
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Contents_ModelGroup : System.Web.UI.Page
{
    protected int GroupID = 0;
    protected string Action = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        GroupID = NetRequest.GetQueryInt("ModelGroupFormsID");
        Action = NetRequest.GetQueryString("Action");

        if (!IsPostBack)
        {
            Whir.BLL.Whir_Module moduleBll = new Whir.BLL.Whir_Module();

            ddlSelect.DataSource = moduleBll.GetList();
            ddlSelect.DataTextField = "moduleName";
            ddlSelect.DataValueField = "moduleMark";
            ddlSelect.DataBind();

            BindModels();
            switch (Action)
            {
                case "Add":
                    break;
                case "Modify":
                    InitControlValue();
                    btnSumbit.Text = "修改";
                    break;
                case "Delete":
                    InitControlValue();
                    btnSumbit.Text = "删除";
                    break;
            }
        }
    }

    private void InitControlValue()
    {
        Whir.BLL.Whir_ModelForms bll = new Whir.BLL.Whir_ModelForms();
        Whir.Model.Whir_ModelForms model = new Whir.Model.Whir_ModelForms();
        model = bll.GetModel(GroupID);

        string groupIDs = model.FormsGroup;
        this.txtGroupName.Text = model.GroupName;
        this.ddlSelect.SelectedValue = model.ModuleMark;

        Whir.BLL.Whir_Module moduleBll = new Whir.BLL.Whir_Module();
        ddlModel1.DataSource = moduleBll.GetModelTable(this.ddlSelect.SelectedValue);
        ddlModel1.DataTextField = "modelName";
        ddlModel1.DataValueField = "modelID";
        ddlModel1.DataBind();
        ddlModel1.Items.Insert(0, new ListItem(" == 请选择 == ", "none"));

        this.ddlModel1.SelectedValue = groupIDs.Split(',')[0].Split('|')[0].ToString();
        ddlModelForm1.DataSource = BindModelForms(int.Parse(this.ddlModel1.SelectedValue));
        ddlModelForm1.DataTextField = "formName";
        ddlModelForm1.DataValueField = "modelFormID";
        ddlModelForm1.DataBind();
        this.ddlModelForm1.SelectedValue = groupIDs.Split(',')[0].Split('|')[1].ToString();


        ddlModel2.DataSource = moduleBll.GetModelTable(this.ddlSelect.SelectedValue);
        ddlModel2.DataTextField = "modelName";
        ddlModel2.DataValueField = "modelID";
        ddlModel2.DataBind();
        ddlModel2.Items.Insert(0, new ListItem(" == 请选择 == ", "none"));
        this.ddlModel2.SelectedValue = groupIDs.Split(',')[1].Split('|')[0].ToString();
        ddlModelForm2.DataSource = BindModelForms(int.Parse(this.ddlModel2.SelectedValue));
        ddlModelForm2.DataTextField = "formName";
        ddlModelForm2.DataValueField = "modelFormID";
        ddlModelForm2.DataBind();
        this.ddlModelForm2.SelectedValue = groupIDs.Split(',')[1].Split('|')[1].ToString();

        if (groupIDs.Split(',').Length > 2)
        {
            ddlModel3.DataSource = moduleBll.GetModelTable(this.ddlSelect.SelectedValue);
            ddlModel3.DataTextField = "modelName";
            ddlModel3.DataValueField = "modelID";
            ddlModel3.DataBind();
            ddlModel3.Items.Insert(0, new ListItem(" == 请选择 == ", "none"));
            this.ddlModel3.SelectedValue = groupIDs.Split(',')[2].Split('|')[0].ToString();
            ddlModelForm3.DataSource = BindModelForms(int.Parse(this.ddlModel3.SelectedValue));
            ddlModelForm3.DataTextField = "formName";
            ddlModelForm3.DataValueField = "modelFormID";
            ddlModelForm3.DataBind();
            this.ddlModelForm3.SelectedValue = groupIDs.Split(',')[2].Split('|')[1].ToString();
        }

    }

    protected void BindModels()
    {
        Whir.BLL.Whir_Module moduleBll = new Whir.BLL.Whir_Module();

        ddlModel1.DataSource = moduleBll.GetModelTable(this.ddlSelect.SelectedValue);
        ddlModel1.DataTextField = "modelName";
        ddlModel1.DataValueField = "modelID";
        ddlModel1.DataBind();
        ddlModel1.Items.Insert(0, new ListItem(" == 请选择 == ", "none"));

        ddlModel2.DataSource = moduleBll.GetModelTable(this.ddlSelect.SelectedValue);
        ddlModel2.DataTextField = "modelName";
        ddlModel2.DataValueField = "modelID";
        ddlModel2.DataBind();
        ddlModel2.Items.Insert(0, new ListItem(" == 请选择 == ", "none"));

        ddlModel3.DataSource = moduleBll.GetModelTable(this.ddlSelect.SelectedValue);
        ddlModel3.DataTextField = "modelName";
        ddlModel3.DataValueField = "modelID";
        ddlModel3.DataBind();
        ddlModel3.Items.Insert(0, new ListItem(" == 请选择 == ", "none"));
    }

    protected DataTable BindModelForms(int modelID)
    {
        Whir.BLL.Whir_ModelForm bll = new Whir.BLL.Whir_ModelForm();
        DataTable dt = bll.GetModelForms(modelID);
        return dt;
    }

    protected void ddlModel1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlModel1.SelectedValue == "none")
        {
            ddlModelForm1.Items.Clear();
        }
        else
        {
            DataTable dt = BindModelForms(int.Parse(ddlModel1.SelectedItem.Value));
            if (dt.Rows.Count > 0)
            {
                ddlModelForm1.DataSource = dt;
                ddlModelForm1.DataTextField = "formName";
                ddlModelForm1.DataValueField = "modelFormID";
                ddlModelForm1.DataBind();
            }
            else
            {
                JsHelper.redirect(Page, "未检测到该功能模块的表单，现跳转至功能模块管理页面！", "ModelManage.aspx");
            }
        }
    }

    protected void ddlModel2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlModel2.SelectedValue == "none")
        {
            ddlModelForm2.Items.Clear();
        }
        else
        {
            DataTable dt = BindModelForms(int.Parse(ddlModel2.SelectedItem.Value));
            if (dt.Rows.Count > 0)
            {
                ddlModelForm2.DataSource = dt;
                ddlModelForm2.DataTextField = "formName";
                ddlModelForm2.DataValueField = "modelFormID";
                ddlModelForm2.DataBind();
            }
            else
            {
                JsHelper.redirect(Page, "未检测到该功能模块的表单，现跳转至功能模块管理页面！", "ModelManage.aspx");
            }
        }
    }

    protected void ddlModel3_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlModel3.SelectedValue == "none")
        {
            ddlModelForm3.Items.Clear();
        }
        else
        {
            DataTable dt = BindModelForms(int.Parse(ddlModel3.SelectedItem.Value));
            if (dt.Rows.Count > 0)
            {
                ddlModelForm3.DataSource = dt;
                ddlModelForm3.DataTextField = "formName";
                ddlModelForm3.DataValueField = "modelFormID";
                ddlModelForm3.DataBind();
            }
            else
            {
                JsHelper.redirect(Page, "未检测到该功能模块的表单，现跳转至功能模块管理页面！", "ModelManage.aspx");
            }
        }
    }

    protected void btnSumbit_Click(object sender, EventArgs e)
    {
        string message = "";
        string model1 = ddlModel1.SelectedValue;
        string model2 = ddlModel2.SelectedValue;
        string model3 = ddlModel3.SelectedValue;

        string modelForm1 = ddlModelForm1.SelectedValue;
        string modelForm2 = ddlModelForm2.SelectedValue;
        string modelForm3 = ddlModelForm3.SelectedValue;

        string formsGroup = "";

        if (model1 == "none" || model2 == "none")
        {
            JsHelper.Alert(Page, "请至少选择两个功能模块，以确认对应关系！");
            return;
        }

        if (model3 == "none")
            formsGroup = model1 + "|" + modelForm1 + "," + model2 + "|" + modelForm2;
        else
            formsGroup = model1 + "|" + modelForm1 + "," + model2 + "|" + modelForm2 + "," + model3 + "|" + modelForm3;

        Whir.BLL.Whir_ModelForms bll = new Whir.BLL.Whir_ModelForms();
        Whir.Model.Whir_ModelForms model;

        switch (Action)
        {
            case "Add":
                model = new Whir.Model.Whir_ModelForms();
                model.ModuleMark = ddlSelect.SelectedValue;
                model.GroupName = txtGroupName.Text.Trim();
                model.FormsGroup = formsGroup;
                bll.Add(model);
                message = "功能模块关系添加成功！";
                break;
            case "Modify":
                model = bll.GetModel(GroupID);
                model.ModuleMark = ddlSelect.SelectedValue;
                model.GroupName = txtGroupName.Text.Trim();
                model.FormsGroup = formsGroup;
                bll.Update(model);
                message = "功能模块关系更新成功！";
                break;
            case "Delete":
                model = bll.GetModel(GroupID);
                if (model != null)
                {
                    bll.Delete(GroupID);
                }
                message = "功能模块关系删除成功！";
                break;
        }
        JsHelper.redirect(Page, message, "ModelGroupManage.aspx");
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ModelGroupManage.aspx");
    }

    protected void ddlSelect_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindModels();
    }
}
